var document = this.document;

const PARAMS = {
    node: { key: 'node', label: '将', valueType: 'string', defaultValue: '' },
    row: { key: 'start', label: '第', labelAfter: '行', valueType: 'number', defaultValue: 0 },
    topOrButtom: {
        key: 'topOrButtom',
        dropdown: [
            { label: '顶端', value: 'top' },
            { label: '底端', value: 'bottom' },
        ],
        valueType: 'string',
        defaultValue: 'top',
    },
};

const types = {
    type: 'LIST_BOX_KIT',
    icon: 'icon-widget-list-viewer',
    title: '列表框套件',
    version: '1.0.0',
    author: 'XJ王大哥(2357942846)',
    isInvisibleWidget: true,
    isGlobalWidget: true,
    properties: [],
    methods: [
        {
            key: 'scroll',
            params: [PARAMS.node, { ...PARAMS.row, label: '滚动至' }],
            blockOptions: { color: 'rgba(0, 175, 195, 1)', line: '滚动' },
        },
        {
            key: 'scrollTopOrButtom',
            params: [PARAMS.node, { ...PARAMS.topOrButtom, label: '滚动至' }],
            blockOptions: { color: 'rgba(0, 175, 195, 1)' },
        },
        {
            key: 'visibility',
            valueType: 'number',
            params: [PARAMS.node, { ...PARAMS.topOrButtom, label: '可见区域', labelAfter: '行' }],
            blockOptions: { color: 'rgba(0, 175, 195, 1)' },
        },
    ],
    events: [],
};

class Widget extends InvisibleWidget {
    constructor(p) {
        super(p);
        Object.assign(this, p);
    }

    setNode(node) {
        this.node = document.querySelector(`#${node}`).childNodes[0];
        this.nodeList = [...this.node.childNodes];
    }

    scroll(node, row) {
        this.setNode(node);
        row--;

        this.node.scrollTop = this.nodeList[row].offsetTop;
    }

    scrollTopOrButtom(node, topOrButtom) {
        this.setNode(node);
        const buttom = topOrButtom === 'bottom';

        this.node.scrollTop = buttom ? this.node.scrollHeight : 0;
    }

    visibility(node, topOrButtom) {
        this.setNode(node);
        const buttom = topOrButtom === 'bottom';

        const scrollTop = this.node.scrollTop + (buttom ? this.node.offsetHeight : 0);
        return this.nodeList.findIndex(v => v.offsetTop + (buttom ? v.offsetHeight : 0) >= scrollTop) + 1;
    }
}

for (let i of types.methods) i.blockOptions = { ...i.blockOptions, callMethodLabel: false };

exports.types = types;
exports.widget = Widget;
